﻿using System;
using System.Collections;
using System.Reflection;
using System.Xml;

namespace ZYCommon
{
    public class StringDataFormat
    {
        private ArrayList myItems = new ArrayList();
        private string strLastTestResult = null;

        public void Clear()
        {
            this.myItems.Clear();
        }

        public bool FromXML(XmlElement myElement)
        {
            this.myItems.Clear();
            if (myElement != null)
            {
                foreach (XmlNode node in myElement.ChildNodes)
                {
                    if (node is XmlElement)
                    {
                        StringDataFormatItem item = StringDataFormatItem.Create(node.Name);
                        if (item != null)
                        {
                            this.myItems.Add(item);
                            item.FromXML(node as XmlElement);
                        }
                    }
                }
                return true;
            }
            return false;
        }

        public bool HasItem()
        {
            return ((this.myItems != null) && (this.myItems.Count > 0));
        }

        public StringDataFormatItem NewItem(string strName)
        {
            StringDataFormatItem item = StringDataFormatItem.Create(strName);
            if (item != null)
            {
                this.myItems.Add(item);
            }
            return item;
        }

        public bool Test(string strValue)
        {
            bool flag=false;
            this.strLastTestResult = null;
            if (!this.HasItem())
            {
                return true;
            }
            try
            {
                foreach (StringDataFormatItem item in this.myItems)
                {
                    if (!item.Test(strValue))
                    {
                        this.strLastTestResult = item.DisplayName;
                        return false;
                    }
                }
                flag = true;
            }
            catch
            {
            }
            return flag;
        }

        public bool ToXML(XmlElement myElement)
        {
            if (myElement != null)
            {
                foreach (StringDataFormatItem item in this.myItems)
                {
                    if (item.Name != null)
                    {
                        XmlElement newChild = myElement.OwnerDocument.CreateElement(item.Name);
                        myElement.AppendChild(newChild);
                        item.ToXML(newChild);
                    }
                }
                return true;
            }
            return false;
        }

        public StringDataFormatItem this[int index]
        {
            get
            {
                return (StringDataFormatItem) this.myItems[index];
            }
        }

        public ArrayList Items
        {
            get
            {
                return this.myItems;
            }
        }

        public string LastTestResult
        {
            get
            {
                return this.strLastTestResult;
            }
        }
    }
}

